﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameJolt.Async;
#endregion

namespace GameJoltServices.Requests
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class LogInRequest : AsyncRequestBase<LoggedInUser>
	{
		#region Properties
		public ServicesManager Manager
		{
			get;
			private set;
		}

		public override string GameID
		{
			get
			{
				return Manager.GameID;
			}
			set
			{
			}
		}

		protected override string PrivateKey
		{
			get
			{
				return Manager.PrivateKey;
			}
			set
			{
			}
		}

		public string Username
		{
			get;
			private set;
		}

		public string UserToken
		{
			get;
			private set;
		}

		public UserIndex UserIndex
		{
			get;
			private set;
		}
		#endregion

		#region Constructors
		public LogInRequest(ServicesManager manager, string username, string userToken, UserIndex userIndex) : base(null, null)
		{
			this.Manager = manager;
			this.Username = username;
			this.UserToken = userToken;
		}
		#endregion

		#region Methods
		public override LoggedInUser Process(IAsyncResult result)
		{
			if(Manager.IsUsingDevData)
			{
				var tokens = UserToken.Split(' ');
				var username = this.Username;
				var type = "User";
				var status = "Active";
				foreach(var token in tokens)
				{
					if(token.StartsWith("type"))
						type = token.Substring(token.IndexOf(':') + 1);
					else if(token.StartsWith("status"))
						status = token.Substring(token.IndexOf(':') + 1);
					else if(token.StartsWith("username"))
						username = token.Substring(token.IndexOf(':') + 1);
				}

				return new LoggedInUser(Manager, new GameJolt.User
				{
					Username = username,
					Type = type,
					Status = status,
					SignedUp = "1 Month ago",
					LastLoggedIn = "Online Now",
					DeveloperName = type == "Developer" ? "Fake Games Inc." : string.Empty,
					DeveloperWebsite = type == "Developer" ? "fakegamesinc.com" : string.Empty,
					DeveloperDescription = type == "Developer" ? "We're not a real game development company." : string.Empty,
				}, UserToken);
			}

			var authResult = GameJolt.User.AuthenticateUser(GameID, PrivateKey, Username, UserToken);
			if(!GameJolt.RequestHelper.WasSuccessful(authResult))
				return null;

			var rawUser = GameJolt.User.GetByUsername(GameID, PrivateKey, Username);
			return new LoggedInUser(Manager, rawUser, UserToken, UserIndex);
		}
		#endregion
	}
}
